<template>
   <div>
      <el-breadcrumb separator="/">
         <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
         <el-breadcrumb-item><a href="javascript:;">物流管理</a></el-breadcrumb-item>
         <el-breadcrumb-item>物流列表</el-breadcrumb-item>
      </el-breadcrumb>

      <div class="hengxian"></div>

      <el-row>
         <el-col :span="15">
            <el-button type="primary" size="mini" @click="dialogTableCreate = true">
               <i class="el-icon-plus"></i><span>新增</span>
            </el-button>
         </el-col>

         <el-col :span="5">
            <div>
               <el-input placeholder="请输入内容" v-model="keyword" class="input-with-select" size="mini">
                  <el-select v-model="cate" slot="prepend" placeholder="请选择">
                     <el-option label="ID" value="id"></el-option>
                     <el-option label="名称" value="name"></el-option>
                  </el-select>
                  <el-button slot="append" @click="handleSearch" icon="el-icon-search"></el-button>
               </el-input>
            </div>
         </el-col>
      </el-row>

      <el-table :data="expresses" style="width: 100%;margin-bottom: 20px;" row-key="id" border
                default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
         <el-table-column label="ID" prop="id" width="90"></el-table-column>
         <el-table-column label="快递名称" prop="name"></el-table-column>
         <el-table-column label="快递网址" prop="url">
            <template slot-scope="scope">
               <a :href="scope.row.url" target="_blank">{{scope.row.url}}</a>
            </template>
         </el-table-column>
         <el-table-column label="快递描述" prop="description"></el-table-column>
         <el-table-column label="运费 / 满额包邮" prop="">
            <template slot-scope="scope">{{scope.row.shipping_money}} / {{scope.row.shipping_free}}</template>
         </el-table-column>
         <el-table-column prop="is_enable" label="是否可用" width="80">
            <template slot-scope="scope">
               <i :class="scope.row.is_enable ? 'el-icon-success' : 'el-icon-error'"
                  @click="handleChangeAttr(scope.row)"></i>
            </template>
         </el-table-column>
         <el-table-column label="排序" prop="sort_order">
            <template slot-scope="scope">
               <el-input-number v-model="scope.row.sort_order"
                                @change="handleChange(scope.row.id,scope.row.sort_order)"
                                :min="1" :max="999" size="mini"></el-input-number>
            </template>
         </el-table-column>

         <el-table-column label="操作">
            <template slot-scope="scope">
               <i class="el-icon-edit" @click="handleEdit(scope.$index,scope.row)"></i>
               <i class="el-icon-delete" @click="handleDelete(scope.$index,scope.row)"></i>
            </template>
         </el-table-column>
      </el-table>

      <!--分页-->
      <div class="page">
         <span class="total">共 {{page.total}} 条数据</span>
         <el-pagination background :total="page.total" :page-size="page.size" @current-change="handleCurrentChange"
                        :current-page.sync="page.currentPage" layout="prev, pager, next">
         </el-pagination>
      </div>

      <!--新增快递模态框-->
      <el-dialog title="新增快递" :visible.sync="dialogTableCreate" @close='closeDialog'>
         <el-form :model="express" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="快递名称" prop="name">
               <el-input v-model="express.name"></el-input>
            </el-form-item>
            <el-form-item label="快递公司代码" prop="code">
               <el-input v-model="express.code"></el-input>
            </el-form-item>
            <el-form-item label="网址" prop="url">
               <el-input type="url" v-model="express.url"></el-input>
            </el-form-item>
            <el-form-item label="运费" prop="shipping_money">
               <el-input v-model="express.shipping_money"></el-input>
            </el-form-item>
            <el-form-item label="满额包邮" prop="shipping_free">
               <el-input v-model="express.shipping_free"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort_order">
               <el-input-number v-model="express.sort_order" @change="handleCreateChange" :min="1"
                                :max="999"></el-input-number>
            </el-form-item>
            <el-form-item label="描述" prop="description">
               <el-input type="textarea" v-model="express.description"></el-input>
            </el-form-item>
            <el-form-item label="是否可用" prop="is_enable">
               <el-switch v-model="express.is_enable"></el-switch>
            </el-form-item>
            <el-form-item>
               <el-button type="primary" size="mini" @click="submitForm('ruleForm')">保存</el-button>
               <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
         </el-form>
      </el-dialog>

      <!--编辑快递-->
      <el-dialog title="编辑物流" :visible.sync="dialogTableEdit" @close='closeDialog'>
         <el-form :model="expressEdit" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="快递名称" prop="name">
               <el-input v-model="expressEdit.name"></el-input>
            </el-form-item>
            <el-form-item label="快递公司代码" prop="code">
               <el-input v-model="expressEdit.code"></el-input>
            </el-form-item>
            <el-form-item label="网址" prop="url">
               <el-input type="url" v-model="expressEdit.url"></el-input>
            </el-form-item>
            <el-form-item label="运费" prop="shipping_money">
               <el-input v-model="expressEdit.shipping_money"></el-input>
            </el-form-item>
            <el-form-item label="满额包邮" prop="shipping_free">
               <el-input v-model="expressEdit.shipping_free"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort_order">
               <el-input-number v-model="expressEdit.sort_order" @change="handleCreateChange" :min="1"
                                :max="999"></el-input-number>
            </el-form-item>
            <el-form-item label="描述" prop="description">
               <el-input type="textarea" v-model="expressEdit.description"></el-input>
            </el-form-item>
            <el-form-item label="是否可用" prop="is_enable">
               <el-switch v-model="expressEdit.is_enable"></el-switch>
            </el-form-item>
            <el-form-item>
               <el-button type="primary" @click="updateForm('ruleForm')" size="mini">更新</el-button>
               <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
            </el-form-item>
         </el-form>
      </el-dialog>

   </div>
</template>

<script>
   export default {
      name: "Home",
      data() {
         return {
            loading: false,
            keyword: '',
            cate: '',
            expresses: [],
            dialogTableCreate: false,
            express: {
               name: '',
               code: '',
               url: '',
               shipping_money: '',
               shipping_free: '',
               sort_order: 999,
               description: '',
               is_enable: true,
            },
            expressEdit: {},
            rules: {
               name: [
                  {required: true, message: '请输入快递名称', trigger: 'blur'},
                  {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
               ],
            },
            page: {
               total: 0,  //总数
               size: 0,  //每页显示数
               currentPage: 1,
            },
            dialogTableEdit: false,
         }
      },
      created() {
         this.init();
      },
      methods: {
         //搜索
         handleSearch() {
            this.init();
         },
         //分页--获取当前页
         handleCurrentChange(val) {
            // console.log(`当前页: ${val}`);
            this.page.currentPage = val;
            this.init();
         },
         //改变排序
         handleCreateChange(value) {
            // console.log(value);
            this.express.sort_order = value;
         },
         //首页
         init() {
            this.loading = true;
            axios.get(`/admin/shop/expresses?page=${this.page.currentPage}&cate=${this.cate}&keyword=${this.keyword}`)
               .then((res) => {
                  // console.log(res.data);
                  this.expresses = res.data.data;
                  this.page.total = res.data.total;
                  this.page.size = res.data.per_page;
                  this.loading = false;
               })
         },
         //改变属性
         handleChangeAttr(row) {
            axios.patch(`/admin/shop/expresses/change_attr`, {id: row.id, is_enable: row.is_enable})
               .then((res) => {
                  this.$notify({
                     title: '成功',
                     message: '改变状态成功',
                     type: 'success'
                  });
                  this.init();
               })
         },
         //排序
         handleChange(id, sort_order) {
            axios.patch(`/admin/shop/expresses/change_sort`, {id: id, sort_order: sort_order})
               .then((res) => {
                  this.$notify({
                     title: '成功',
                     message: '排序成功！',
                     type: 'success',
                  });
                  this.init();
               })
         },
         //删除
         handleDelete(index, row) {
            this.$confirm('此操作将永久删除该快递, 是否继续?', '提示', {
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type: 'warning',
               center: true
            }).then(() => {
               axios.delete(`/admin/shop/expresses/${row.id}`)
                  .then((res) => {
                     // console.log(res);
                     if (res.data.status == 1) {
                        this.expresses.splice(index, 1);
                        this.$message({
                           type: 'success',
                           message: res.data.msg,
                        });
                        this.init();
                     } else {
                        this.$message.error(res.data.msg);
                     }

                  });
            }).catch(() => {
               this.$message({
                  type: 'info',
                  message: '已取消删除'
               });
            });
         },
         //关闭模态框,先清空验证再清除表单
         closeDialog() {
            this.$refs.ruleForm.clearValidate();
            this.$refs.ruleForm.resetFields();
         },
         //新增
         submitForm(formName) {
            this.$refs[formName].validate((valid) => {
               if (valid) {
                  axios.post(`/admin/shop/expresses`, this.express)
                     .then((res) => {
                        this.$message({
                           showClose: true,
                           message: '新增快递成功',
                           type: 'success'
                        });
                        this.dialogTableCreate = false;
                        this.init();
                        this.$refs[formName].resetFields();  //重置表单
                     })
               } else {
                  this.$message({
                     showClose: true,
                     message: '新增失败',
                     type: 'error'
                  });
                  return false;
               }
            });
         },
         resetForm(formName) {
            this.$refs[formName].resetFields();
         },
         //编辑模态框
         handleEdit(index, row) {
            axios.get(`/admin/shop/expresses/${row.id}`)
               .then((res) => {
                  this.dialogTableEdit = true;
                  this.expressEdit = res.data;
                  this.expressEdit.is_enable = !!res.data.is_enable;
               })
         },
         //更新
         updateForm(formName) {
            this.$refs[formName].validate((valid) => {
               if (valid) {
                  axios.put(`/admin/shop/expresses/${this.expressEdit.id}`, this.expressEdit)
                     .then((res) => {
                        this.$message({
                           showClose: true,
                           message: '编辑快递成功',
                           type: 'success'
                        });
                        this.dialogTableEdit = false;
                        this.init();
                     })
               } else {
                  console.log('error submit!!');
                  return false;
               }
            });
         },
      }
   }
</script>

<style>

</style>